package com.lq.controller;

import com.lq.pojo.Article;
import com.lq.pojo.Category;
import com.lq.pojo.Comment;
import com.lq.pojo.User;
import com.lq.service.ArticleService;
import com.lq.service.CategoryService;
import com.lq.service.CommentService;
import com.lq.service.TagService;
import com.lq.util.SystemLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private TagService tagService;

    //获取所有文章与其分类
    @RequestMapping("/article")
    public String articleManage(Model model) {

        List<Article> articleList = articleService.getAllArticle();

        List<Category> categoryList = categoryService.getAllCategory();

        model.addAttribute("articleList", articleList);
        model.addAttribute("categoryList", categoryList);

        return "article";
    }

    //查询特定文章
    @RequestMapping("/browseArticle")
    public String getArticleById(String articleId, Model model) {

        int aId = Integer.parseInt(articleId);

        Article article = articleService.getArticleById(aId);

        model.addAttribute("article", article);

        return "showArticle";
    }

    @RequestMapping("/deleteArticle")
    public String deleteArticle(String articleId, Model model) {

        int aId = Integer.parseInt("articleId");

        //删除文章并删除该文章下的所有评论
        if (articleService.deleteArticleById(aId) && articleService.deleteAllArticleComment(aId)) {
            model.addAttribute("isDeleteArticleSuccess", true);
        }

        SystemLogUtil.deleteArticleLog();

        return "forward:/article";
    }

    @RequestMapping("/getEditArticle")
    public String getEditArticle(String aId, Model model) throws ServletException, IOException {

        //在修改文章之前获得用户需要修改的文章

        int articleId = Integer.parseInt(aId);

        Article article = articleService.getArticleById(articleId);

        List<Category> categoryList = categoryService.getAllCategory();

        model.addAttribute("categoryList", categoryList);
        model.addAttribute("article", article);

        return "editArticle";
    }

    @RequestMapping("/getLatestArticle")
    public String getLatestArticle(Model model) {

        List<Article> latestArticleList = articleService.getLatestArticle(5);

        model.addAttribute("latestArticleList", latestArticleList);

        return "forward:/getLatestComment";
    }

    @RequestMapping("/preview")
    public String preview(Model model, String aId) {

        int articleId = Integer.parseInt(aId);

        Article article = articleService.getArticleById(articleId);

        int commentNum = commentService.getCommentNumByArticleId(articleId);
        String category = articleService.getArticleCategoryByArticleId(articleId);
        List<Comment> commentList = commentService.getCommentsByArticleId(articleId);

        model.addAttribute("article", article);
        model.addAttribute("category", category);
        model.addAttribute("commentNum", commentNum);
        model.addAttribute("commentList", commentList);

        return "preview";
    }

    //发布文章
    @RequestMapping("/publish")
    public String publish(Article article, HttpSession session, Model model) {

        /**
         * 判断发布文章的页面是否选中四个个checkbox
         * 未选中checkbox不返回值为null，给值为0
         * 若选中checkbox则返回值不为null，给值1
         */

        int canComment = 1;//默认可以评论
        int canPing = 1;//默认可以ping
        int canSubscribe = 1;//默认可以订阅
        int isDraft = 0;//默认发布文章

        if (article != null) {
            article.setCanComment(0); //禁止评论

            article.setCanPing(0);
            article.setCanSubscribe(0);
            article.setCanPing(1);//存为草稿

        }

        User user = (User) session.getAttribute("user");

        //保存文章标签到tag表
        tagService.saveTag(article.getArticleTag(), user.getUserId());

        Article temp = new Article(
                0,
                article.getArticleName(),
                article.getArticleContent(),
                article.getArticleTag(),
                article.getArticleCategory(),
                null,
                user.getUserId(),
                canComment,
                canPing,
                canSubscribe,
                1,
                isDraft
        );

        //插入文章成功返回ture
        if (articleService.publishArticle(article, user.getUserId())) {
            model.addAttribute("isPublishSuccess", true);
        }

        SystemLogUtil.publishArticleLog();

        return "forward:/getCategory";
    }

    @RequestMapping("/updateArticle")
    public String updateArticle(Article article, Integer aId, Integer category, HttpSession session) throws ServletException, IOException {

        User user = (User) session.getAttribute("user");

        //保存文章标签到tag表
        tagService.saveTag(article.getArticleTag(), user.getUserId());

        articleService.updateArticleByArticleId(article, aId);

        SystemLogUtil.publishArticleLog();

        return "forward:/article";
    }
}
